/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.GiuongDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Tu Nguyen
 */
public class GiuongDAO {
     
      
      public static ResultSet TimGheCuaToa(String MaToa)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Select * From giuong Where toa ='%s'",MaToa);
            
            PreparedStatement ps = con.prepareStatement(sql);            
            ResultSet rs = ps.executeQuery();
            
           return rs;
        }
        catch (Exception ex)
        {
           return null;
        }
      
    }
      public static boolean UpdateTinhTrang(String magiuong, int tinhtrang)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Update Giuong set tinh_trang=%s Where so_hieu ='%s'",tinhtrang, magiuong);
            
            PreparedStatement ps = con.prepareStatement(sql);            
            int n = ps.executeUpdate();
           return true;
        }
        catch (Exception ex)
        {
           return false;
        }
      
    }
        public static GiuongDTO Get(String magiuong, String maphong)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Select * From giuong Where so_hieu ='%s' and phong='%s'", magiuong,maphong);
            
            PreparedStatement ps = con.prepareStatement(sql);            
            ResultSet rs = ps.executeQuery();
            GiuongDTO gi = new GiuongDTO();
            while(rs.next())
            {
                gi.gia = rs.getFloat("gia");
                gi.so_hieu = rs.getString("so_hieu");
                gi.phong = rs.getString("phong");
            }
           return gi;
        }
        catch (Exception ex)
        {
           return null;
        }
      
    }
      
       public static ResultSet TimGiuongTheoPhong(String phong, int tang)
    {
        try
        {
            Connection con = DBConnect.Connect();
                String sql = String.format("Select * From giuong Where phong ='%s' and tang='%d'",phong,tang);
            
            PreparedStatement ps = con.prepareStatement(sql);            
            ResultSet rs = ps.executeQuery();
            
           return rs;
        }
        catch (Exception ex)
        {
           return null;
        }
      
    }
}
